<template>
  <div>
    <div class="merchart-chart">
      <month-condition class="month-condition" @change="query" />
      <div ref="chart" :class="className" :style="{height:height,width:width}" />
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons')
import resize from '../../mixins/resize'

export default {
  components: {
    MonthCondition: () => import('./MonthCondition')
  },
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    query(monthDate) {
      const param = {
        monthDate
      }
      this.get('/merchant/getMerchantSalePriceTop10', param, { showLoading: false }).then(ret => {
        this.setOptions({
          xAxisData: ret.merchantNames,
          seriesData: ret.merchantSalePrices
        })
      })
    },
    initChart() {
      this.chart = echarts.init(this.$refs.chart, 'macarons')
      this.setOptions()
    },
    setOptions({ xAxisData = [], seriesData = [] } = {}) {
      this.chart.clear()
      this.chart.setOption({
        title: {
          text: '设备销售额排行Top10',
          bottom: '2',
          left: 'center',
          textStyle: {
            color: '#999'
          }
        },
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          left: '80px',
          right: '80px'
        },
        xAxis: [
          {
            type: 'category',
            data: xAxisData,
            axisPointer: {
              type: 'shadow'
            },
            axisLabel: {
              color: '#999'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '销售额（元）',
            nameTextStyle: {
              color: '#999'
            },
            axisLabel: {
              color: '#999',
              formatter: '{value}'
            }
          }
        ],
        series: [
          {
            name: '销售额',
            type: 'bar',
            data: seriesData
          }
        ]
      })
    }
  }
}
</script>
